package com.example.guesswho.data.parse

import com.parse.ParseObject
import com.parse.ParseUser
import java.util.Date

data class Question(
    val objectId: String,
    val title: String,
    val coverUrl: String,
    val voiceUrl: String?,
    val options: List<String>,
    val answerIndex: Int,
    val category: String,
    val difficulty: Int,
    val isAudited: Boolean
) {
    companion object {
        fun fromParseObject(parseObject: ParseObject): Question {
            return Question(
                objectId = parseObject.objectId,
                title = parseObject.getString("title") ?: "",
                coverUrl = parseObject.getString("coverUrl") ?: "",
                voiceUrl = parseObject.getString("voiceUrl"),
                options = parseObject.getList("options")?.filterIsInstance<String>() ?: emptyList(),
                answerIndex = parseObject.getInt("answerIndex"),
                category = parseObject.getString("category") ?: "",
                difficulty = parseObject.getInt("difficulty"),
                isAudited = parseObject.getBoolean("isAudited") ?: false
            )
        }
        
        fun toParseObject(): ParseObject {
            val parseObject = ParseObject("Question")
            parseObject.put("title", title)
            parseObject.put("coverUrl", coverUrl)
            voiceUrl?.let { parseObject.put("voiceUrl", it) }
            parseObject.put("options", options)
            parseObject.put("answerIndex", answerIndex)
            parseObject.put("category", category)
            parseObject.put("difficulty", difficulty)
            parseObject.put("isAudited", isAudited)
            return parseObject
        }
    }
}

data class Room(
    val objectId: String,
    val difficulty: Int,
    val status: String,   // waiting|playing|ended
    val users: List<ParseUser>,
    val unansweredCount: Int,
    val createdAt: Date
) {
    companion object {
        fun fromParseObject(parseObject: ParseObject): Room {
            return Room(
                objectId = parseObject.objectId,
                difficulty = parseObject.getInt("difficulty"),
                status = parseObject.getString("status") ?: "waiting",
                users = parseObject.getList("users")?.filterIsInstance<ParseUser>() ?: emptyList(),
                unansweredCount = parseObject.getInt("unansweredCount"),
                createdAt = parseObject.createdAt ?: Date()
            )
        }
        
        fun toParseObject(): ParseObject {
            val parseObject = ParseObject("Room")
            parseObject.put("difficulty", difficulty)
            parseObject.put("status", status)
            parseObject.put("users", users)
            parseObject.put("unansweredCount", unansweredCount)
            return parseObject
        }
    }
}

data class AudienceChat(
    val objectId: String,
    val roomId: String,
    val username: String,
    val text: String,
    val ts: Long
) {
    companion object {
        fun fromParseObject(parseObject: ParseObject): AudienceChat {
            return AudienceChat(
                objectId = parseObject.objectId,
                roomId = parseObject.getString("roomId") ?: "",
                username = parseObject.getString("username") ?: "",
                text = parseObject.getString("text") ?: "",
                ts = parseObject.getLong("ts")
            )
        }
        
        fun toParseObject(): ParseObject {
            val parseObject = ParseObject("AudienceChat")
            parseObject.put("roomId", roomId)
            parseObject.put("username", username)
            parseObject.put("text", text)
            parseObject.put("ts", ts)
            return parseObject
        }
    }
}

data class ShortLink(
    val objectId: String,
    val code: String,  // 4位
    val roomId: String
) {
    companion object {
        fun fromParseObject(parseObject: ParseObject): ShortLink {
            return ShortLink(
                objectId = parseObject.objectId,
                code = parseObject.getString("code") ?: "",
                roomId = parseObject.getString("roomId") ?: ""
            )
        }
        
        fun toParseObject(): ParseObject {
            val parseObject = ParseObject("ShortLink")
            parseObject.put("code", code)
            parseObject.put("roomId", roomId)
            return parseObject
        }
    }
}